package com.example.yx.demo19;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.GenericApplicationContext;

import java.util.List;

public class A19 {

    @Aspect
    static class MyAspect {
        @Before("execution(* foo(..))") //静态通知调用,不带参数绑定,执行时需要切点
        public void before1() {
            System.out.println("before1");
        }

        @Before("execution(* foo(..)) && args(x)") //动态通知调用，需要参数绑定，执行时需要切点用于处理参数
        public void before2(int x) {
            System.out.printf("before2(%d)%n",x);
        }
    }

    static class Target {
        public void foo(int x) {
            System.out.printf("target foo(%d)%n",x);
        }
    }

    @Configuration
    static class MyConfig{
        @Bean
        AnnotationAwareAspectJAutoProxyCreator proxyCreator() {return new AnnotationAwareAspectJAutoProxyCreator();}

        @Bean
        public MyAspect myAspect() {return new MyAspect();}
    }

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.registerBean(MyConfig.class);
        context.refresh();

        AnnotationAwareAspectJAutoProxyCreator creator = context.getBean(AnnotationAwareAspectJAutoProxyCreator.class);
//        List<Advisor> list = creator.

        Target target = new Target();
        ProxyFactory factory = new ProxyFactory();
        factory.setTarget(target);
//        factory.addAdvisors(list);
        Object proxy = factory.getProxy();

//        List<Object> intreceptorList = factory.getInterceptorsAndDynamicInterceptionAdvice(Target.class.getMethod("foo", int.class),Target.class);
//        for (Object o : intreceptorList) {
//            System.out.println(o);
//        }
    }
}
